﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace Worktool
{
    public partial class MaterialDetail : Form
    {
        public MaterialDetail()
        {
            InitializeComponent();
            this.Text = "Material Details: " + Properties.Settings.Default.CurrentDBName; 
        }

        private void MaterialDetail_Load(object sender, EventArgs e)
        {
            this.vw_eng_MaterialDetailTableAdapter.Fill(this.aSCDataSet.vw_eng_MaterialDetail);

        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Clipboard.GetText() != "")
            {
                for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
                {
                    try
                    {
                        dataGridView1.SelectedCells[i].Value = Clipboard.GetText();
                    }
                    catch (Exception ex)
                    { }
                }
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count == 1)
            {
                Clipboard.SetText(dataGridView1.SelectedCells[0].Value.ToString());
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
                try
                {

                    this.vwengMaterialDetailBindingSource.EndEdit();
                    vw_eng_MaterialDetailTableAdapter.Update(aSCDataSet);

                }
                catch (Exception ex)
                {
                    
                }
            
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "" && textBox2.Text == "")
            {
                vwengMaterialDetailBindingSource.Filter = null;
            }
            else if (textBox1.Text == "" && textBox2.Text != "")
            {
                vwengMaterialDetailBindingSource.Filter = string.Format("Manufacturer LIKE '{0}'", (textBox2.Text + "*"));
            }
            else if (textBox1.Text != "" && textBox2.Text == "")
            {
                vwengMaterialDetailBindingSource.Filter = string.Format("Venue LIKE '{0}'", (textBox1.Text + "*"));
            }
            else if (textBox1.Text != "" && textBox2.Text != "")
            {
                vwengMaterialDetailBindingSource.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}'", (textBox1.Text + "*"), (textBox2.Text + "*"));
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "" && textBox2.Text == "")
            {
                vwengMaterialDetailBindingSource.Filter = null;
            }
            else if (textBox1.Text == "" && textBox2.Text != "")
            {
                vwengMaterialDetailBindingSource.Filter = string.Format("Manufacturer LIKE '{0}'", (textBox2.Text + "*"));
            }
            else if (textBox1.Text != "" && textBox2.Text == "")
            {
                vwengMaterialDetailBindingSource.Filter = string.Format("Venue LIKE '{0}'", (textBox1.Text + "*"));
            }
            else if (textBox1.Text != "" && textBox2.Text != "")
            {
                vwengMaterialDetailBindingSource.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}'", (textBox1.Text + "*"), (textBox2.Text + "*"));
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
            {
                try
                {
                    if (dataGridView1.SelectedCells[i].ValueType == typeof(string))
                    {
                        dataGridView1.SelectedCells[i].Value = "";
                    }
                    if (dataGridView1.SelectedCells[i].ValueType == typeof(int))
                    {
                        dataGridView1.SelectedCells[i].Value = 0;
                    }
                    if (dataGridView1.SelectedCells[i].ValueType == typeof(DateTime))
                    {
                        dataGridView1.SelectedCells[i].Value = "";
                    }
                }
                catch (Exception ex)
                { }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            dataGridView1.ClearSelection();
        }
    }
}
